package com.example.xinhuazidian

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.View.OnClickListener
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.xinhuazidian.adapter.IdiomAdapter
import com.example.xinhuazidian.bean.Idiom
import com.example.xinhuazidian.databinding.ActivityCyactivityBinding
import com.example.xinhuazidian.utils.JsonFileUtil
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.gyf.immersionbar.ImmersionBar
import com.yechaoa.yutilskt.YUtils
import java.lang.reflect.Type

/**
 * 成语
 */
class CYActivity : AppCompatActivity(), OnClickListener {

    private lateinit var binding: ActivityCyactivityBinding
    private lateinit var mapBeanList: MutableList<Idiom>
    private var idiomAdapter: IdiomAdapter? = null
    private var isWord = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCyactivityBinding.inflate(layoutInflater)
        setContentView(binding.root)

        load()
        initView()
        initData()
    }

    private fun initView() {
        //沉浸状态栏
        ImmersionBar.with(this)
            .statusBarDarkFont(true)//状态栏字体是深色，不写默认为亮色
            .init()

        binding.btnSs.setOnClickListener(this)
    }

    private fun initData() {

    }

    @SuppressLint("SetTextI18n")
    private fun load() {
        val str = JsonFileUtil.loadFile(this, "idiom.json")

        val mapBeanListType: Type = object : TypeToken<ArrayList<Idiom?>?>() {}.type
        mapBeanList = Gson().fromJson(str, mapBeanListType)

        binding.tvNum.text = "一共${mapBeanList.size}个成语"
        initAdapter(mapBeanList)
    }

    private fun initAdapter(list: MutableList<Idiom>) {
        //设置布局排列方式，默认垂直排列
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rv.layoutManager = linearLayoutManager

        //加载适配器
        idiomAdapter = IdiomAdapter(list)
        binding.rv.adapter = idiomAdapter
    }

    @SuppressLint("SetTextI18n")
    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.btn_ss -> {
                YUtils.showLoading(this, "加载中...")

                isWord = false

                for (i in mapBeanList.indices) {
                    if (mapBeanList[i].word.toString().substring(0, 1) == binding.et.text.toString()
                            .trim()
                    ) {
                        isWord = true
                    }
                }

                if (TextUtils.isEmpty(binding.et.text.toString().trim())) {
                    load()
                } else if (isWord) {
                    val listOfList = mutableListOf<Idiom>()

                    for (i in mapBeanList.indices) {
                        if (mapBeanList[i].word.toString()
                                .substring(0, 1) == binding.et.text.toString().trim()
                        ) {
                            listOfList.add(mapBeanList[i])
                        }
                    }

                    binding.tvNum.text = "查到${listOfList.size}个成语"
                    initAdapter(listOfList)
                    YUtils.hideLoading()
                } else {
                    Toast.makeText(this, "没有查到这个字开头的成语哦", Toast.LENGTH_SHORT).show()
                    YUtils.hideLoading()
                }
            }
        }
    }
}